/*
 * Copyright 2004-2005 companyname
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.basedata.dao;

import java.util.List;

import org.apache.log4j.Logger;

import com.companyname.common.basedata.model.Type;
import com.companyname.common.basedata.util.CompanyNameBaseDataException;
import com.companyname.common.base.dao.BaseDAOHibernate;

/**
 * <p>Title: 类别</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: companyname</p>
 * @ $Author: author $
 * @ $Date: 2005/02/16 08:27:39 $
 * @ $Revision: 1.17 $
 * @ created in 2006-11-13
 *
 */

public class TypeDAOimp extends BaseDAOHibernate
                             implements TypeDAO
{
        static Logger logger = Logger.getLogger(TypeDAOimp.class);

        /**
         * 描述:查询类别s
         *
         * @ param:
         * @ param:orderBy 排序属性（for hql）
         * @ param:sortType 排序方式（asc or desc）
         * @ Exception:
         * @ return model集合
         */
        public List findTypes(Type superType,String keyWord,String orderBy,String sortType)
        {
                if(superType != null)
                {
                        String hql = "from Type " +
                                     " where superType = ? " +
                                     " and (code like ? or  name like ?) " +
                                     " order by " + orderBy + " " + sortType;
                        Object[] values = new Object[3];
                        values[0] = superType;
                        values[1] = "%" + keyWord + "%";
                        values[2] = "%" + keyWord + "%";
                        return this.getHibernateTemplate().find(hql,values);
                }
                else
                {
                        String hql = " from Type " +
                                     " where (code like ? or  name like ?) " +
                                     " order by " + orderBy + " " + sortType;
                        Object[] values = new Object[2];
                        values[0] = "%" + keyWord + "%";
                        values[1] = "%" + keyWord + "%";
                        return this.getHibernateTemplate().find(hql,values);
                }
                
        }

        public Type getByCode(String code)
        {
                String hql = "from Type where code = ? ";
                Object[] values = new Object[] {code};
                List types = this.getHibernateTemplate().find(hql,values);
                if(types.size() > 1)
                {
                        throw new CompanyNameBaseDataException("有" + types.size() + "个类别的code为" + code);
                }
                if(types.size() == 0)
                {
                        return null;
                }
                return (Type)types.get(0);
        }

}
